package com.bdqn.util;

import lombok.Getter;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * SpringContext
 *
 * @author LILIBO
 * @since 2024/10/25
 */
@Component
public class SpringContext implements ApplicationContextAware {

    @Getter
    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContext.applicationContext = applicationContext;
    }

    /**
     * 适用于SpringBean使用注解@Service("xxxService")获取接口对象
     *
     * @param beanName 例如：xxxService
     * @return
     */
    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    /**
     * 适用于SpringBean使用注解
     *
     * @param clazz 例如：xxxService.class
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

}
